﻿DO $$
 DECLARE
   familinomo_arr text[] := '{"Ivanov", "Petrov", "Sidorov", "Valuev", "Homer"}';
   unua_nomo_arr text[] := '{"Ivan", "Petr", "Sidor", "Alex", "Poli"}';
   dua_nomo_arr text[] := '{"Ivanovich", "Antony", "Mark", "Igorevich", "Valerievich"}';
   poshta_num numeric := random();
   skip_emails text[] := '{"test@test.te", "test2@test.te", "test3@test.te", "admin4@admin.ru", "admin3@admin.ru", "admin2@admin2.ru"}';
   rec RECORD;

  BEGIN
    FOR rec IN SELECT * FROM uzantoj LOOP
      UPDATE uzantoj
      SET familinomo = jsonb_set(to_jsonb(rec.familinomo), '{enhavo}', to_jsonb(familinomo_arr[1 + floor((random() * array_length(familinomo_arr, 1)))])),
      unua_nomo = jsonb_set(to_jsonb(rec.unua_nomo), '{enhavo}', to_jsonb(unua_nomo_arr[1 + floor((random() * array_length(unua_nomo_arr, 1)))])),
      dua_nomo = jsonb_set(to_jsonb(rec.dua_nomo), '{enhavo}', to_jsonb(dua_nomo_arr[1 + floor((random() * array_length(dua_nomo_arr, 1)))])),
      chefa_telefonanumero = '+0 000 000 000 000',
      chefa_retposhto = 'test' || poshta_num || '@test.test',
      password = 'Do not know'
      WHERE id = rec.id AND rec.chefa_retposhto <> ALL (skip_emails);

      UPDATE uzantoj
      SET password = 'pbkdf2_sha256$260000$8feT1Nv0jIjs930HnqBGTZ$EifAG9Evk5C0uDTPdvBqpdb00jFcTshcosIK/5QKcSQ='
      WHERE id = rec.id AND rec.chefa_retposhto = ANY (skip_emails);

      poshta_num = poshta_num + 1;
    END LOOP;
END $$;


